package cn.aitrox.ry.app.web.controller.image;

import cn.aitrox.ry.app.web.config.AppNacosProperties;
import cn.aitrox.ry.app.web.service.ImageTransferService;
import cn.aitrox.ry.app.web.vo.image.transfer.ImageTransferExecuteInVo;
import cn.aitrox.ry.app.web.vo.image.transfer.ImageTransferPauseInVo;
import cn.aitrox.ry.app.web.vo.image.transfer.ImageTransferRestartnVo;
import cn.aitrox.ry.app.web.vo.image.transfer.ImageTransferResultExporttnVo;
import cn.aitrox.ry.app.web.vo.image.transfer.ImageTransferResultQueryInVo;
import cn.aitrox.ry.app.web.vo.image.transfer.ImageTransferResultQueryOutVo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.common.bean.ExportFileCallback;
import cn.aitrox.ry.common.controller.BaseController;
import cn.aitrox.ry.enumtype.ProcessStatusEnum;
import cn.aitrox.ry.service.image.dto.image.transfer.out.ImageTransferResultExportDataDto;
import cn.aitrox.ry.util.ArrayUtil;
import cn.aitrox.ry.util.ExcelUtil;
import cn.aitrox.ry.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class ImageTransferController extends BaseController {

    @Autowired
    private AppNacosProperties appNacosProperties;

    @Autowired
    private ImageTransferService imageTransferService;

    @PostMapping("/image/transfer/execute")
    public RespResult execute(@Validated @RequestBody ImageTransferExecuteInVo input) {
        return imageTransferService.execute(input.getTaskId());
    }

    @PostMapping("/image/transfer/result/query")
    public RespResult<ImageTransferResultQueryOutVo> resultQuery(@Validated @RequestBody ImageTransferResultQueryInVo input) {
        return imageTransferService.resultQuery(input.getTaskId());
    }

    @PostMapping("/image/transfer/restart")
    public RespResult restart(@Validated @RequestBody ImageTransferRestartnVo input) {
        return imageTransferService.restart(input.getTaskId());
    }

    @PostMapping("/image/transfer/pause")
    public RespResult pause(@Validated @RequestBody ImageTransferPauseInVo input) {
        return imageTransferService.pause(input.getTaskId());
    }

    @PostMapping("/image/transfer/result/export")
    public ResponseEntity resultExport(@Validated @RequestBody ImageTransferResultExporttnVo input) throws IOException {

        List<ImageTransferResultExportDataDto> data = imageTransferService.resultExportData(input.getTaskId());

        List<List<String>> exportData = this.packExportData(data);
        String fileName = "转换结果.xlsx";
        File tmpParentFile = new File(appNacosProperties.getTmpFileParentPath() + UUIDUtil.getUUID());
        File exportFile = new File(tmpParentFile, fileName);

        return super.exportFileUseByte(tmpParentFile, fileName, exportFile, new ExportFileCallback() {
            @Override
            public File writeTmpFile(File exportFile) throws IOException {
                ExcelUtil.writeFile(exportData, exportFile);
                return exportFile;
            }

        });
    }

    private List<List<String>> packExportData(List<ImageTransferResultExportDataDto> data) {
        List<List<String>> result = new ArrayList<>();
        result.add(Arrays.asList("序列号", "结果", "异常原因"));

        if (ArrayUtil.isEmpty(data)) {
            return result;
        }

        data.forEach(x -> {
            result.add(Arrays.asList(x.getSeriesUid(), ProcessStatusEnum.getEnum(x.getStatus()).getDesc(), x.getErrMsg() + ""));
        });

        return result;
    }
}
